package com.zzyl.nursing.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * 日期工具类
 */
public class DateUtils {
    
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    
    /**
     * 根据出生日期计算年龄
     * 
     * @param birthday 出生日期字符串，格式为 yyyy-MM-dd
     * @return 年龄
     */
    public static int getAge(String birthday) {
        if (birthday == null || birthday.isEmpty()) {
            return 0;
        }
        
        try {
            LocalDate birthDate = LocalDate.parse(birthday, DATE_FORMATTER);
            LocalDate currentDate = LocalDate.now();
            return Period.between(birthDate, currentDate).getYears();
        } catch (Exception e) {
            return 0;
        }
    }
    
    /**
     * 根据出生日期计算年龄
     * 
     * @param birthday 出生日期
     * @return 年龄
     */
    public static int getAge(LocalDate birthday) {
        if (birthday == null) {
            return 0;
        }
        
        LocalDate currentDate = LocalDate.now();
        return Period.between(birthday, currentDate).getYears();
    }
    
    /**
     * 根据出生日期计算年龄
     * 
     * @param birthday 出生日期时间
     * @return 年龄
     */
    public static int getAge(LocalDateTime birthday) {
        if (birthday == null) {
            return 0;
        }
        
        LocalDate birthDate = birthday.toLocalDate();
        LocalDate currentDate = LocalDate.now();
        return Period.between(birthDate, currentDate).getYears();
    }
}
